package cn.bone.search.web;


import cn.bone.search.bean.NewsDo;
import cn.bone.search.dao.NewsDao;
import cn.bone.search.dao.SearchDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by superbone on 2017/3/20.
 */
@RestController
@RequestMapping("/search")
public class SearchController {

    @Resource
    private NewsDao newsDao;
    @Resource
    private SearchDao searchDao;

    private Logger logger = LoggerFactory.getLogger("索引");

    @RequestMapping("/index")
    public boolean index(){

        long start = System.currentTimeMillis();
        List<NewsDo> list = newsDao.list(500);
        long middle = System.currentTimeMillis();
        boolean result = searchDao.indexList(list);
        long end = System.currentTimeMillis();

        long queryTime = middle-start;
        long indexTime = end - middle;
        logger.info("查询数据耗时:"+queryTime);
        logger.info("存储索引耗时:"+indexTime);

        return result;
    }

    @RequestMapping("/query")
    public List<NewsDo> query(String keyword,Integer page,Integer num){
        if(page==null){
            page = 1;
        }
        if(num==null){
            num = 10;
        }
        int start = (page-1)*num;

        long startTime = System.currentTimeMillis();
        List<NewsDo> list = searchDao.query(keyword, start, num);
        long endTime = System.currentTimeMillis();
        long queryTime = endTime-startTime;
        logger.info("查询数据耗时:"+queryTime);
        return list;
    }
}
